﻿using System;
using System.Windows;
using YwangHRMSys.DAL;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.LoginMgr
    {
        /// <summary>
        ///     LoginWindowEdit.xaml 的交互逻辑
        /// </summary>
        public partial class LoginWindowEdit : Window
            {

                public LoginWindowEdit()
                {
                    InitializeComponent();
                }

                public bool IsInsert { get; set; }
                public Guid EditnGuid { get; set; }

                private void Window_Loaded( object sender, RoutedEventArgs e )
                {
                    if (IsInsert)
                        {
                        }
                    else
                        {
                            //编辑时，将所有的值从数据库传入到文本框中
                            var dal = new OperatorInfoDAL();
                            OperatorInfoModel op = dal.GetById(EditnGuid);
                            TxtUserNameBox.Text = op.UserName;
                            TxtRealNameBox.Text = op.RealName;
                        }
               }


                #region 添加编辑修改信息
             
                /// <summary>
                ///     添加编辑修改信息
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void BtnSave_Click( object sender, RoutedEventArgs e )
                {
                    string txtPassword = TxtPasswordBox.Password;
                    string txtUserName = TxtUserNameBox.Text;
                    string txtRealName = TxtRealNameBox.Text;
                    string md5Password = CommonHelper.GetMD5(txtPassword + CommonHelper.GetPasswordSalt());

                   
                    if (txtRealName == "" || txtUserName == "")
                        {
                            MessageBox.Show("用户名和真实姓名不能为空");
                            return;

                        }
                    if (IsInsert)
                        {
                            OperatorInfoModel operatorInfo = new OperatorInfoDAL().GetByUserName(txtUserName);
                            if (operatorInfo != null)
                                {
                                    if (txtUserName == operatorInfo.UserName)
                                        {
                                            MessageBox.Show("用户名重复！\n请重新输入");
                                            return;
                                        }

                                }
                            if (txtPassword == "")
                                {
                                    if (MessageBox.Show("当密码为空时，默认添加密码为000000", "警告", MessageBoxButton.YesNo)
                                        == MessageBoxResult.Yes)
                                        {
                                            var oep = new OperatorInfoModel();
                                            oep.Id = Guid.NewGuid();
                                            oep.UserName = txtUserName;
                                            oep.RealName = txtRealName;
                                            const string newpassword = "000000";
                                            oep.PassWord = CommonHelper.GetMD5
                                                    (newpassword + CommonHelper.GetPasswordSalt());

                                            new OperatorInfoDAL().InsertOperatorInfo(oep);
                                            DialogResult = true;
                                            return;
                                        }
                                    return;
                                }
                            if (txtPassword.Length < 6)
                            {
                                MessageBox.Show("密码长度不能低于6位");
                                return;
                            }
                            var op = new OperatorInfoModel();
                            op.Id = Guid.NewGuid();
                            op.UserName = txtUserName;
                            op.RealName = txtRealName;
                            op.PassWord = md5Password;
                            new OperatorInfoDAL().InsertOperatorInfo(op);
                            DialogResult = true;
                        }
                    else
                        {
                            if (txtPassword.Length <= 0) //当密码栏为空时，不输入密码
                                {
                                    new OperatorInfoDAL().Update(EditnGuid, txtUserName, txtRealName);
                                new AdminPermissionDal().UpdateAdminPermission(EditnGuid,txtUserName);
                                }
                            else //当密码栏不为空，则把密码修改为用户输入的密码
                                {
                                    new OperatorInfoDAL().Update(EditnGuid, txtUserName, txtRealName, md5Password);
                                    new AdminPermissionDal().UpdateAdminPermission(EditnGuid, txtUserName);
                             
                                }
                            DialogResult = true;
                        }
                }
                #endregion
                //关闭对话框
                private void BtnCancel_Click( object sender, RoutedEventArgs e )
                {
                    DialogResult = false;
                }

            }
    }